package com.liujit.auth.domain.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.liujit.common.constants.AuthConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.oauth2.common.OAuth2AccessToken;

/**
 * @author: liujun
 * @date: 2021/6/21 11:19 下午
 * @description: 自定义token
 */
@NoArgsConstructor
@Data
@ApiModel(value = "OauthToken", description = "自定义token")
@JsonPropertyOrder({ "accessToken", "refreshToken", "tokenType", "expiresIn" })
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class OauthToken {

    @ApiModelProperty(value = "访问令牌")
    private String accessToken;

    @ApiModelProperty(value = "刷新令牌")
    private String refreshToken;

    @ApiModelProperty(value = "令牌类型")
    private String tokenType = AuthConstant.BEARER.trim();

    @ApiModelProperty(value = "有效时间（秒）")
    private int expiresIn;

    public static OauthToken accessToken(String accessToken) {
        OauthToken oauthToken = new OauthToken();
        oauthToken.setAccessToken(AuthConstant.BEARER + accessToken);
        return oauthToken;
    }

    public static OauthToken accessToken(OAuth2AccessToken oAuth2AccessToken) {
        OauthToken oauthToken = new OauthToken();
        oauthToken.setAccessToken(AuthConstant.BEARER + oAuth2AccessToken.getValue());
        oauthToken.setRefreshToken(oAuth2AccessToken.getRefreshToken().getValue());
        oauthToken.setTokenType(oAuth2AccessToken.getTokenType());
        oauthToken.setExpiresIn(oAuth2AccessToken.getExpiresIn());
        return oauthToken;
    }
}
